<?php
namespace Web\Model;
use Think\Model;
class WebUserModel extends Model{
    /**
     * 下一页评估
     * @return [type] [1无业，2有职业]
     *
     * A
     * 下一步评估
     * $_post
     * array(
     * 0=>工薪贷
     * 1=>123,456,789(地区)
     * 2=>小额贷款,银行
     * 3=>array(
     *     0=>array(
     *         0=>房产抵用类
     *         1=>子类，。。。
     *     )
     *     1=>array(
     *         0=>工薪抵用类
     *         1=>子类,...
     *     )
     * )
     * )
     *
     *
     * array(
     * 0=>array(
     *     0=>'0'
     *     1=>'只有职业'
     * )
     * 1=>123,456,789(地区)
     * 2=>小额贷款,银行
     * 3=>array(
     *     0=>array(
     *         0=>房产抵用类
     *         1=>子类，。。。
     *     )
     *     1=>array(
     *         0=>工薪抵用类
     *         1=>子类,...
     *     )
     * )
     * )
     *
     * B
     *
     * $_post
     * array(
     * 0=>工薪贷
     * 1=>123,456,789(地区)
     * 2=>小额贷款,银行
     * 3=>array(
     *         0=>房产抵用类,工薪抵用类
     *         1=>子类，子类2。。。
     * )
     * )
     *
     *
     * array(
     * 0=>array(
     *     0=>'0'
     *     1=>'只有职业'
     * )
     * 1=>123,456,789(地区)
     * 2=>小额贷款,银行
     * 3=>array(
     *         0=>房产抵用类,工薪抵用类
     *         1=>155,15。。。
     * )
     * 4=>
     * 5=>
     * 6=>
     * 7=>
     * 8=>array(
     *      0 =>array(
     *         0=>'小类id',
     *         1=>array(
     *           'formdata' =>
     *               array(
     *                  0=>array(
     *                    'md_id' =>13,
     *                    'input_type' =>1,
     *                    'm_group' =>'155_0',
     *                    'maindata' =>123,
     *                    'altdata' =>'小于等于',
     *                    'bq_id' =>'155',
     *                     )
     *               )
     *           )
     *       )
     *    )
     *
     *9=>array(
     *   0=>array(
     *     // 'yuqi_check_if'=>'',
     *      'yuqi_check_count_cycle'=>'2',
     *    //  'yuqi_check_count_method'=>'9',
     *      'yuqi_check_count_time'=>'9',
     *      'yuqi_check_count_source'=>'9',
     *    ),
     *    1=>array(
     *    //  'yuqi_check_if'=>'',
     *      'yuqi_check_count_cycle'=>'2',
     *    //  'yuqi_check_count_method'=>'9',
     *      'yuqi_check_count_time'=>'9',
     *      'yuqi_check_count_source'=>'9',
     *    ),
     *    2=>array(
     *    //  'yuqi_check_if'=>'',
     *      'yuqi_check_count_cycle'=>'2',
     *    //  'yuqi_check_count_method'=>'9',
     *      'yuqi_check_count_time'=>'9',
     *      'yuqi_check_count_source'=>'9',
     *    )
     * )
     * $type是否只有职业1是，2不是,$arr 数据,$switch是否结果查询
     */
    public function next_pingu($type=2,$arr,$switch=''){
          if($type==1){
          $arr[0][1]='不能进件';
          $where_qudao_goods['jinjian_wuye']=array('notlike','%'.$arr[0][1].'%');
          }
          elseif ($type==2) {
          $where_qudao_goods['shourshenfen']=array('like',array('%'.$arr[0].'%','%两者皆可%'));
          }
          $where_qudao['qudao_type']=array('in',$arr[2]);
          $data=M('qudao')->where($where_qudao)->field('qudao_id')->select();
          foreach ($data as $key => $value) {
              $qudao_id=$qudao_id.$value['qudao_id'];
              if($key<count($data)-1){
                  $qudao_id.=',';
              }

          }
          //地区限定处理
          if($qudao_id){
            $where_qudao_goods['dtypename']=array('in',$qudao_id);
          }
          $where_qudao_goods['a.is_show']=1;
          $str=explode(',', $arr[3][1]);
          foreach ( $str as $key => $value) {
              $str[$key]="%$value%";
          }
          $where_qudao_goods['cp_temp']=array('like', $str);


          $area=explode(',',$arr[1]);
          $where_qudao_goods['u_pro_id']=re_region($area[0]);
          $where_qudao_goods['u_city_id']=re_region($area[1]);

        /***************联表查询核心数据和基础信息********/
        if($switch){
          $where_qudao_goods['qudao_pro_id']=re_region($area[0]);
          $where_qudao_goods['qudao_city_id']=re_region($area[1]);
           /**************进件***************/
            if($arr[4]){
              $res_temp=explode(',', $arr[4]);
              foreach ($res_temp as $key => $value) {
                  $res_temp[$key]="%$value%";
              }
              $where_qudao_goods['jinjian']=array('like', $res_temp);
            }
           /**************进件***************/
           /******************客户要求筛选********************/
           if(!empty($arr[6])){
              foreach ($arr[6] as $key => $value) {
                unset($res_temp);
                $res_temp=explode(',', $arr[6][$key]);
                foreach ($res_temp as $k => $v) {
                    $res_temp[$k]="%$v%";
                }
                $where_qudao_goods[$key]=array('like', $res_temp);
              }
           }
           /******************客户要求筛选********************/
          /******************按揭要求筛选********************/
          if(!empty($arr[7])){
              foreach ($arr[7] as $key => $value) {
                unset($res_temp);
                $screen_arr=array('handling','return','collection');
                if(in_array($key, $screen_arr)){
                   $temp_list_lx=M('lx');
                    if($key=='handling'){
                        if($value==0){
                           $where_lx_temp['handling']=array('eq','0.00');
                        }
                        // else{
                        //    $where_lx_temp['handling']=array('gt',0);
                        // }
                        // 反选处理
                    }
                    if($key=='return'){
                        if($value==1){
                           $where_lx_temp['return']=array('gt',0);
                        }
                        // else{
                        //    $where_lx_temp['return']=array('eq',0);
                        // }
                        // 反选处理
                    }
                    if($key=='collection'){
                        if($value==1){
                           $where_lx_temp['collection']=array('gt',0);
                        }
                        // else{
                        //    $where_lx_temp['collection']=array('eq',0);
                        // }
                        // 反选处理
                    }
                    $group_g_id=$temp_list_lx->where($where_lx_temp)->group('g_id')->select();
                    foreach ($group_g_id as $group_key => $group_value) {
                          $string_g_uid=$string_g_uid.$group_value['g_id'];
                          if($group_key<count($group_g_id)-1){
                              $string_g_uid.=',';
                          }
                    }
                    if(!$string_g_uid){
                      $string_g_uid='-1';
                    }
                    $where_qudao_goods['a.id']=array('IN',$string_g_uid);
                }
                else{
                  $res_temp=explode(',', $arr[7][$key]);
                  foreach ( $res_temp as $k => $v) {
                      $res_temp[$k]="%$v%";
                  }
                  $where_qudao_goods[$key]=array('like', $res_temp);
                }
              }
          }
          /******************按揭要求筛选********************/
          /****************精确搜索处理**********************/
          if(!empty($arr[8])){

              unset($res_temp);
              $res_temp=D('WebUser')->m_data($arr);
              $where_qudao_goods['cpclass']=array('like', $res_temp);
          }
          /****************精确搜索处理**********************/
          /*****************客户年龄对应产品筛选******************/
          if(session('my_kuhu.kehu_old')>0){
              $age_oa['age_min']=array('Elt', session('my_kuhu.kehu_old'));
              $age_oa['age_max']=array('Egt', session('my_kuhu.kehu_old'));
              $age_oa['_logic']='and';
              $where_qudao_goods['_complex']=$age_oa;
          }
          /*****************客户年龄对应产品筛选******************/
          /****************征信筛选**********************/
          if(!empty($arr[9][0])){

              unset($res_temp);
              $res_temp=D('WebUser')->ck_zx($arr);
              $where_qudao_goods['cinv_times_id']=array('in', $res_temp);
            }
          if(!empty($arr[9][1])){

              unset($res_temp);
              $res_temp1=D('WebUser')->ck_zx_see($arr);
              $where_qudao_goods['cinv_see_id']=array('in', $res_temp1);
          }
          /****************征信筛选**********************/
          $res_data=M('goods')
                      ->alias('a')
                     ->join('bm_goods_foundation b  ON  a.gx_id=b.gx_id','LEFT')
                     ->join('bm_user c ON a.u_id=c.u_id')
                     ->join("bm_qudao d ON b.qudaoname=d.qudao_nick_name and d.qudao_pro_id={$where_qudao_goods['qudao_pro_id']} and d.qudao_city_id={$where_qudao_goods['qudao_city_id']}",'LEFT')
                     ->join('bm_goods_diy_name e ON a.id=e.g_id','LEFT')
                     ->join('bm_cinv_times f ON f.g_id=a.id','LEFT')
                     ->join('bm_cinv_see i ON i.g_id=a.id','LEFT')
                     ->join('bm_lx g ON a.id=g.g_id','LEFT')
                     ->field('a.*,b.dtypename,b.qudaoname,c.u_pro_id,c.u_city_id,d.qudao_id,e.goods_diy_name_id,e.goods_name_reason,e.state as diy_state,d.qudao_pro_id,d.qudao_city_id')
                     ->where($where_qudao_goods)
                     ->group('a.id')
                     ->select();
                      foreach ($res_data as $key => $value) {
                          $where['g_id']=$value['id'];
                          $temp=M('lx')->where($where)->select();
                          $my_temp['min_lx']=$temp[0]['min_lx'];
                          $my_temp['max_lx']=$temp[0]['max_lx'];
                          $my_temp['min_ed']=$temp[0]['min_ed'];
                          $my_temp['max_ed']=$temp[0]['max_ed'];
                          $my_temp['handling']=$temp[0]['handling'];
                          $my_temp['installment_fq']=$temp[0]['installment_fq'];
                          $my_temp['fangkuan_time']=$temp[0]['fangkuan_time'];
                          $my_temp['repayments']=$temp[0]['repayments'];

                          foreach ($temp as $k => $v) {
                             if($temp[$key]['min_lx']<$my_temp['min_lx']){
                                $my_temp['min_lx']=$temp[$k]['min_lx'];
                                $my_temp['repayments']=$temp[$k]['repayments'];
                             }
                             if($temp[$key]['max_lx']>$my_temp['max_lx']){
                                $my_temp['max_lx']=$temp[$k]['max_lx'];
                             }
                             if($temp[$key]['min_ed']<$my_temp['min_ed']){
                                $my_temp['min_ed']=$temp[$k]['min_ed'];
                             }
                             if($temp[$key]['max_ed']>$my_temp['max_ed']){
                                $my_temp['max_ed']=$temp[$k]['max_ed'];
                             }
                             if($temp[$key]['installment_fq']>$my_temp['installment_fq']){
                                $my_temp['installment_fq']=$temp[$k]['installment_fq'];
                             }
                             if($temp[$key]['handling']<$my_temp['handling']){
                                $my_temp['handling']=$temp[$k]['handling'];
                             }
                             if($temp[$key]['fangkuan_time']<$my_temp['fangkuan_time']){
                                $my_temp['fangkuan_time']=$temp[$k]['fangkuan_time'];
                             }
                          }
                          $res_data[$key]['my_lx_data']=$my_temp;
                      }

                      /************************排序处理冒泡***************/
                      /**
                       * $order['sort']=0/1 （0顺序，1倒序）
                       * $order['key']=ed/lx (额度/利息)
                       */
                      $order['sort']=$arr[5][0];
                      $order['key']=$arr[5][1];
                      if(is_array($order)&&$res_data&&isset($order['sort'])&&isset($order['key'])){
                           $temp;
                           $temp_index;

                           for ($i=0; $i < count($res_data); $i++) {

                               for ($j=$i; $j <count($res_data)-1 ; $j++) {

                                    if($order['key']=='ed'){
                                        if($order['sort']=='1'){
                                          if($res_data[$i]['my_lx_data']['max_ed']>$res_data[$j+1]['my_lx_data']['max_ed']){
                                             $temp=$res_data[$i];
                                             $res_data[$i]=$res_data[$j+1];
                                             $res_data[$j+1]=$temp;
                                          }
                                        }
                                        elseif($order['sort']=='0'){
                                          if($res_data[$i]['my_lx_data']['max_ed']<$res_data[$j+1]['my_lx_data']['max_ed']){
                                             $temp=$res_data[$i];
                                             $res_data[$i]=$res_data[$j+1];
                                             $res_data[$j+1]=$temp;
                                          }
                                        }
                                    }
                                    elseif($order['key']=='lx'){

                                        if($order['sort']=='1'){
                                          if($res_data[$i]['my_lx_data']['min_lx']<$res_data[$j+1]['my_lx_data']['min_lx']){
                                             $temp=$res_data[$i];
                                             $res_data[$i]=$res_data[$j+1];
                                             $res_data[$j+1]=$temp;
                                          }
                                        }
                                        elseif($order['sort']=='0'){
                                          if($res_data[$i]['my_lx_data']['min_lx']>$res_data[$j+1]['my_lx_data']['min_lx']){
                                             $temp=$res_data[$i];
                                             $res_data[$i]=$res_data[$j+1];
                                             $res_data[$j+1]=$temp;
                                          }
                                        }
                                    }
                               }
                           }

                      }

                      /************************排序处理***************/

                      /*******************沟通渠道渲染****************/
                      $web_user=M('web_user');
                      foreach ($res_data as $key => $value) {
                          if($res_data[$key]['qudao_id']){
                             unset($where);
                             $where['qudao_id']=$res_data[$key]['qudao_id'];
                             $where['bind_id']=array('gt',0);
                             $res_data_temp[$key] =$web_user->where($where)->field('web_city_id')->group('web_city_id')->select();
                          for ($i=0; $i <count($res_data_temp[$key]) ; $i++) {
                                 $where['web_city_id']=$res_data_temp[$key][$i]['web_city_id'];
                                 $where['bind_id']=array('gt',0);
                                 $res_data[$key]['web_user_all'][$i]=$web_user->where($where)->select();

                             }

                          }
                      }
                      /*******************沟通渠道渲染****************/
          }
         else{
            //第二次查询得到第三页面
            $res_data=M('goods')
                      ->alias('a')
                      ->join('bm_goods_foundation b  ON  a.gx_id=b.gx_id','LEFT')
                      ->join('bm_user c ON a.u_id=c.u_id')
                      ->field('a.*,b.dtypename,c.u_pro_id,c.u_city_id')
                      ->where($where_qudao_goods)
                      ->select();
            /*******************小类相同的情况获取数据并集*******************/
            //数组置空重新构造
            $res_data1=$res_data;
            foreach ($res_data1 as $key => $value) {
                $arr1 =explode(',', $arr[3][1]);
                $arr2=explode(',',$res_data1[$key]['cp_temp']);
                $res_data2[$key]['cp_temp']=array_intersect($arr1,$arr2);
                $res_data2[$key]['jinjian']=$res_data1[$key]['jinjian'];
                $res_data2[$key]['dtypename']=$res_data1[$key]['dtypename'];
                if(count($res_data2[$key]['cp_temp'])>1){
                    for($v=0;$v<count($res_data2[$key]['cp_temp'])-1;$v++){
                        $res_data2[count($res_data1)+$v]=$res_data2[$key];
                        for($sb=0;$sb<=count($res_data2[$key]['cp_temp']);$sb++){
                            if($v!=$sb){
                              unset($res_data2[count($res_data1)+$v]['cp_temp'][$sb]);

                            }
                        }
                    }
                    for ($i=0;$i<count($res_data2[$key]['cp_temp']);$i++){
                       unset($res_data2[$key]['cp_temp'][$i]);
                    }
                }

                /*******************开启数组转换字符串***************/
                foreach ($res_data2 as $key => $value) {
                        foreach ($value['cp_temp'] as $k => $v) {
                           $res_data2[$key]['cp_temp']= $res_data2[$key]['cp_temp'][$k];
                         }
                }
                /*******************开启数组转换字符串***************/


            }

            unset($res_data);
            $res_data=$res_data2;
            sort($res_data);
           /**************获取根据重复部分获取二维数组然后json格式花去重****************/
           for ($i=0; $i <count($res_data) ; $i++) {
              $res_data_o[$i]['cp_temp']=$res_data[$i]['cp_temp'];
              $res_data_o[$i]['dtypename']=$res_data[$i]['dtypename'];
              $res_data_o[$i]=json_encode($res_data_o[$i]);
           }

          $c=array_unique($res_data_o);
          /*************************去重数组重新排列********************************************/
          $ke=0;
          foreach ($c as $key => $value) {
              $res_data_fuyuan[$ke]=json_decode($value,true);
              $ke++;
          }
          /**************************数组拼装****************************************************/
          for ($s=0; $s <count($res_data_fuyuan); $s++) {
            $tmp;
             for ($d=0; $d <count($res_data) ; $d++) {
                 if(($res_data_fuyuan[$s]['cp_temp']==$res_data[$d]['cp_temp'])&&$res_data_fuyuan[$s]['dtypename']==$res_data[$d]['dtypename']){
                    $new_arr[$s]=$res_data_fuyuan[$s];
                    $tmp=$tmp.','.$res_data[$d]['jinjian'];
                 }
             }
             $x_arr[$s]['jinjian']=substr($tmp,1);
             $y_arr[$s]['jinjian']=explode(',',$x_arr[$s]['jinjian']);
             $new_arr[$s]['jinjian']=implode(',', array_unique($y_arr[$s]['jinjian']));
             unset($tmp);
          }

          unset($res_data);
          $res_data=$new_arr;
            /*******************小类相同的情况获取数据并集*******************/
         }
        /***************联表查询核心数据和基础信息********/
        foreach ($res_data as $key => $value) {
            $res_data[$key]['cp_temp']=re_biaoqian($res_data[$key]['cp_temp']);
            $res_data[$key]['jinjian']=re_biaoqian($res_data[$key]['jinjian']);
            $my_nick_where['g_id']=$res_data[$key]['id'];
            $my_nick_where['u_id']=$_SESSION['p_u_id'];
            $red=M('nick_name')->where($my_nick_where)->find();
            if($red){
              $res_data[$key]['goods_nick_name']=$red['nick_name'];
            }
            if(empty($res_data[$key]['jinjian'])){
                unset($res_data[$key]);
            }

        }
        return $res_data;
    }
    /**
     * 数组8处理
     * @return [type] [description]
     */
    public function m_data($arr){
                // 8=>array(
                //      0 =>array(
                //         0=>'小类id',
                //         1=>array(
                //           'formdata' =>
                //               array(
                //                  0=>array(
                //                    'md_id' =>13,
                //                    'input_type' =>1,
                //                    'm_group' =>'155_0',
                //                    'maindata' =>123,
                //                    'altdata' =>'小于等于',
                //                    'bq_id' =>'155',
                //                     )
                //               )
                //           )
                //       )
                //    )
              $m_data_db=M('m_data');
              $out_md_id;
              $dk=0;
              foreach ($arr[8] as $key => $value) {
                  $in_count=count($value[1]['formdata']);
                  $out_count=0;
                  /**
                   * 结构数据和选择重新构造条件
                   * @DateTime:    [2018-03-10 17:53:28]
                   */
                  $where_bq_id['bq_id']=$value[1]['formdata'][0]['bq_id'];
                  $new_arr_m_data=M('m_data')->where($where_bq_id)->select();
                  foreach ($new_arr_m_data as $mk => $mv) {

                     $temp_where_md['md_id']=$mv['md_id'];
                     $temp_m_data=$m_data_db->where($temp_where_md)->find();
                     /**************用户数据处理**********************/

                      switch ($temp_m_data['input_type']) {
                        case '1':
                            if($temp_m_data['altdata']=='小于'){
                                 if($value[1]['formdata'][$dk]['maindata'] < $mv['maindata']){
                                    $out_count++;
                                    $out_data[$mk]['md_id']=$mv['md_id'];
                                 }
                            }
                            elseif($temp_m_data['altdata']=='小于等于'){
                                 if($value[1]['formdata'][$dk]['maindata'] <= $mv['maindata']){
                                    $out_count++;
                                    $out_data[$mk]['md_id']=$mv['md_id'];
                                 }
                            }
                            elseif($temp_m_data['altdata']=='大于'){
                                 if($value[1]['formdata'][$dk]['maindata'] > $mv['maindata']){
                                    $out_count++;
                                    $out_data[$mk]['md_id']=$mv['md_id'];
                                 }
                            }
                            elseif($temp_m_data['altdata']=='大于等于'){

                                 if($value[1]['formdata'][$dk]['maindata'] >= $mv['maindata']){
                                    $out_count++;
                                    $out_data[$mk]['md_id']=$mv['md_id'];
                                 }
                            }
                            elseif($temp_m_data['altdata']=='等于'){
                                 if($value[1]['formdata'][$dk]['maindata'] = $mv['maindata']){
                                    $out_count++;
                                    $out_data[$mk]['md_id']=$mv['md_id'];
                                 }
                            }
                          break;
                        case '2':
                                 if($value[1]['formdata'][$dk]['maindata'] == $mv['maindata']){
                                    $out_count++;
                                    $out_data[$mk]['md_id']=$mv['md_id'];
                                 }
                          break;
                        case '3':
                          if($temp_m_data['altdata']=='任意符合'){
                            $you_arr=explode(',', $mv['maindata']);
                            $my_arr=explode(',', $value[1]['formdata'][$dk]['maindata']);
                            $end=array_udiff($you_arr,$my_arr);
                            if(empty($end)){
                                  $out_count++;
                                  $out_data[$mk]['md_id']=$mv['md_id'];
                            }
                          }
                          elseif($temp_m_data['altdata']=='同时符合'){
                            $you_arr=explode(',', $mv['maindata']);
                            $my_arr=explode(',', $value[1]['formdata'][$dk]['maindata']);
                            $end=array_diff($you_arr,$my_arr);
                            if(empty($end)){
                                  $out_count++;
                                  $out_data[$mk]['md_id']=$mv['md_id'];
                            }
                          }
                          break;
                        default:
                          # code...
                          break;
                      }
                     /**************用户数据处理**********************/

                     if($dk==(count($value[1]['formdata'])-1)){

                        if($in_count==$out_count){
                          $last_out_data[$mk]=$out_data;

                        }
                        $out_count=0;
                        $dk=0;
                        unset($out_data);
                     }
                     else{
                         $dk++;
                     }


                  }
              }
              $k=0;
              /**
               * 重新组合
               * @var integer
               */
              $k1=0;
              foreach ($last_out_data as $k_y => $v_e) {
                  foreach ($v_e as $koy => $voe) {

                      if($last_out_data[$k_y][$koy]['md_id']){
                        $next_my_data[$k1]='%'.$last_out_data[$k_y][$koy]['md_id'].'%';
                      }
                    $k1++;
                  }

              }
              return $next_my_data;
    }
    /**
     * 银行职员查看页面
     * @return [type] [description]
     */
    public function yh_goods_list(){
            $where_qudao_goods['a.is_show']=1;
            $where_qudao_goods['d.qudao_id']=$_SESSION['user']['qudao_id'];
            $where_qudao_goods['u_pro_id']=session('user.web_pro_id');
            $where_qudao_goods['u_city_id']=session('user.web_city_id');
            $where_qudao_goods['qudao_pro_id']=$where_qudao_goods['u_pro_id'];
            $where_qudao_goods['qudao_city_id']=$where_qudao_goods['u_city_id'];
            $res_data=M('goods')
                      ->alias('a')
                     ->join('bm_goods_foundation b  ON  a.gx_id=b.gx_id','LEFT')
                     ->join('bm_user c ON a.u_id=c.u_id')
                     ->join("bm_qudao d ON b.qudaoname=d.qudao_nick_name and d.qudao_pro_id={$where_qudao_goods['qudao_pro_id']} and d.qudao_city_id={$where_qudao_goods['qudao_city_id']}",'LEFT')
                     ->join('bm_goods_diy_name e ON a.id=e.g_id','LEFT')
                     ->field('a.*,b.dtypename,b.qudaoname,c.u_pro_id,c.u_city_id,d.qudao_id,e.goods_diy_name_id,e.goods_name_reason,e.state as diy_state,d.qudao_pro_id,d.qudao_city_id')
                     ->group('id')
                     ->where($where_qudao_goods)
                     ->select();
                      foreach ($res_data as $key => $value) {
                          $where['g_id']=$value['id'];
                          $temp=M('lx')->where($where)->select();
                          $my_temp['min_lx']=$temp[0]['min_lx'];
                          $my_temp['max_lx']=$temp[0]['max_lx'];
                          $my_temp['min_ed']=$temp[0]['min_ed'];
                          $my_temp['max_ed']=$temp[0]['max_ed'];
                          $my_temp['handling']=$temp[0]['handling'];
                          $my_temp['installment_fq']=$temp[0]['installment_fq'];
                          $my_temp['fangkuan_time']=$temp[0]['fangkuan_time'];
                          $my_temp['repayments']=$temp[0]['repayments'];
                          foreach ($temp as $k => $v) {
                             if($temp[$key]['min_lx']<$my_temp['min_lx']){
                                $my_temp['min_lx']=$temp[$k]['min_lx'];
                                $my_temp['repayments']=$temp[$k]['repayments'];
                             }
                             if($temp[$key]['max_lx']>$my_temp['max_lx']){
                                $my_temp['max_lx']=$temp[$k]['max_lx'];
                             }
                             if($temp[$key]['min_ed']<$my_temp['min_ed']){
                                $my_temp['min_ed']=$temp[$k]['min_ed'];
                             }
                             if($temp[$key]['max_ed']>$my_temp['max_ed']){
                                $my_temp['max_ed']=$temp[$k]['max_ed'];
                             }
                             if($temp[$key]['installment_fq']>$my_temp['installment_fq']){
                                $my_temp['installment_fq']=$temp[$k]['installment_fq'];
                             }
                             if($temp[$key]['handling']<$my_temp['handling']){
                                $my_temp['handling']=$temp[$k]['handling'];
                             }
                             if($temp[$key]['fangkuan_time']<$my_temp['fangkuan_time']){
                                $my_temp['fangkuan_time']=$temp[$k]['fangkuan_time'];
                             }
                          }
                          $res_data[$key]['my_lx_data']=$my_temp;
                      }
                      /************************排序处理冒泡***************/
                      /**
                       * $order['sort']=0/1 （0顺序，1倒序）
                       * $order['key']=ed/lx (额度/利息)
                       */
                      $order['sort']=0;
                      $order['key']='ed';
                      if(is_array($order)&&$res_data&&isset($order['sort'])&&isset($order['key'])){
                           $temp;
                           $temp_index;
                           for ($i=0; $i < count($res_data); $i++) {
                               for ($j=$i; $j <count($res_data)-1 ; $j++) {
                                    if($order['key']=='ed'){
                                        if($order['sort']=='1'){
                                          if($res_data[$i]['my_lx_data']['max_ed']<$res_data[$j+1]['my_lx_data']['max_ed']){
                                             $temp=$res_data[$i];
                                             $res_data[$i]=$res_data[$j+1];
                                             $res_data[$j+1]=$temp;
                                          }
                                        }
                                        elseif($order['sort']=='0'){
                                          if($res_data[$i]['my_lx_data']['max_ed']>$res_data[$j+1]['my_lx_data']['max_ed']){
                                             $temp=$res_data[$i];
                                             $res_data[$i]=$res_data[$j+1];
                                             $res_data[$j+1]=$temp;
                                          }
                                        }
                                    }
                                    elseif($order['key']=='lx'){
                                        if($order['sort']=='1'){
                                          if($res_data[$i]['my_lx_data']['min_lx']<$res_data[$j+1]['my_lx_data']['min_lx']){
                                             $temp=$res_data[$i];
                                             $res_data[$i]=$res_data[$j+1];
                                             $res_data[$j+1]=$temp;
                                          }
                                        }
                                        elseif($order['sort']=='0'){
                                          if($res_data[$i]['my_lx_data']['min_lx']>$res_data[$j+1]['my_lx_data']['min_lx']){
                                             $temp=$res_data[$i];
                                             $res_data[$i]=$res_data[$j+1];
                                             $res_data[$j+1]=$temp;
                                          }
                                        }
                                    }

                               }

                           }
                      }

                      /************************排序处理***************/

                      /*******************沟通渠道渲染****************/
                      // $web_user=M('web_user');
                      // foreach ($res_data as $key => $value) {
                      //     if($res_data[$key]['qudao_id']){
                      //        unset($where);
                      //        $where['qudao_id']=$res_data[$key]['qudao_id'];
                      //        $res_data_temp[$key] =$web_user->where($where)->field('web_city_id')->group('web_city_id')->select();
                      //        for ($i=0; $i <count($res_data_temp[$key]) ; $i++) {
                      //            $where['web_city_id']=$res_data_temp[$key][$i]['web_city_id'];
                      //            $res_data[$key]['web_user_all'][$i]=$web_user->where($where)->select();
                      //        }
                      //     }
                      // }
                      /*******************沟通渠道渲染****************/
        foreach ($res_data as $key => $value) {
            $res_data[$key]['cp_temp']=re_biaoqian($res_data[$key]['cp_temp']);
            $res_data[$key]['jinjian']=re_biaoqian($res_data[$key]['jinjian']);
            $my_nick_where['g_id']=$res_data[$key]['id'];
            $my_nick_where['u_id']=$_SESSION['p_u_id'];
            $red=M('nick_name')->where($my_nick_where)->find();
            if($red){
              $res_data[$key]['goods_nick_name']=$red['nick_name'];
            }
            if(empty($res_data[$key]['jinjian'])){
                unset($res_data[$key]);
            }

        }
        return $res_data;
    }

    /**
     * 征信查询
     * 斜率处理
     * 查询次数
     */
    public function ck_zx($arr){
/*      $we['cinv_times_id']=array('IN','1,2,3,4,5,6,9,10,11,12,13');
      $r=M('goods')->alias('a')->join('bm_cinv_times b ON b.g_id=a.id','left')->where($we)->select();
      dump($r);
        $arr[9][0]=array(
          0=>array(
            //'zhengxin_check_if'=>'',
            'zhengxin_check_count_method'=>'信用卡查询',
            'zhengxin_check_count_cycle'=>'2',
            'zhengxin_check_count_time'=>'9'
          ),
          1=>array(
           // 'zhengxin_check_if'=>'',
            'zhengxin_check_count_method'=>'信用卡查询',
            'zhengxin_check_count_cycle'=>'3',
            'zhengxin_check_count_time'=>'1'
          ),
          1=>array(
            //'zhengxin_check_if'=>'',
            'zhengxin_check_count_method'=>'信用卡查询',
            'zhengxin_check_count_cycle'=>'6',
            'zhengxin_check_count_time'=>'6'
          ),
        );*/

        /*****************备选征信数据******************/
        $res_arr=M('cinv_times')->select();
        $in_arr;
        foreach ($res_arr as $key => $value) {
           $sp_pc=$value['zhengxin_check_count_time']/$value['zhengxin_check_count_cycle'];
           foreach ($arr[9][0] as $k => $v) {
               $sp_web=$v['zhengxin_check_count_time']/$v['zhengxin_check_count_cycle'];
               if($sp_web<=$sp_pc){
                  if($key>0&&$key<count($res_arr)){
                    $in_arr.=',';
                  }
                  $in_arr.=$value['cinv_times_id'];
                  break;

               }
           }
        }
        if(!$in_arr){
          $in_arr='-1';
        }
      return $in_arr;
    }

    /**
     * 襁褓处理
     * @DateTime:    [2018-03-15 18:24:24]
     * 逾期
     */
    public function ck_zx_see($arr){
/*        $we['cinv_see_id']=array('IN','17,18');
        $r=M('goods')->alias('a')->join('bm_cinv_see b ON b.g_id=a.id','LEFT')->where($we)->select();
        dump($r);*/
        // $arr[9][1]=array(
        //   0=>array(
        //    // 'yuqi_check_if'=>'',
        //     'yuqi_check_count_cycle'=>'2',
        //   //  'yuqi_check_count_method'=>'9',
        //     'yuqi_check_count_time'=>'9',
        //     'yuqi_check_count_source'=>'9',
        //   ),
        //   1=>array(
        //   //  'yuqi_check_if'=>'',
        //     'yuqi_check_count_cycle'=>'2',
        //   //  'yuqi_check_count_method'=>'9',
        //     'yuqi_check_count_time'=>'9',
        //     'yuqi_check_count_source'=>'9',
        //   ),
        //   2=>array(
        //   //  'yuqi_check_if'=>'',
        //     'yuqi_check_count_cycle'=>'2',
        //   //  'yuqi_check_count_method'=>'9',
        //     'yuqi_check_count_time'=>'9',
        //     'yuqi_check_count_source'=>'9',
        //   ),
        // );
        /*****************备选征信数据******************/
        $res_arr=M('cinv_see')->field('cinv_see_id,yuqi_check_count_cycle,yuqi_check_count_time,yuqi_check_count_source')->select();
        $in_arr;
        foreach ($res_arr as $key => $value) {
           $sp_pc=$value['yuqi_check_count_source'];
           foreach ($arr[9][1] as $k => $v) {
               $sp_web=$v['yuqi_check_count_source'];
               if($sp_web<=$sp_pc&&($value['yuqi_check_count_cycle']==$v['yuqi_check_count_cycle'])&&($value['yuqi_check_count_time']==$v['yuqi_check_count_time'])){
                  if($key>0&&$key<count($res_arr)){
                    $in_arr.=',';
                  }
                  $in_arr.=$value['cinv_see_id'];
                  break;

               }
           }
         }
      if(!$in_arr){
        $in_arr='-1';
      }
      return $in_arr;
    }
  /**
   *
   * 图片上传
   *
   */
  public function upload_img($fileimg){
      $file = $_FILES['file'];
      $name = $file['name'];
      $type = strtolower(substr($name,strrpos($name,'.')+1));
      $allow_type = array('jpg','jpeg','gif','png');
      if(!in_array($type, $allow_type)){
        $mes['code']=2;
        $mes['message']="文件类型不是允许上传类型";
      }
      if(!is_uploaded_file($file['tmp_name'])){
        $mes['code']=6;
        $mes['message']="不是通过HTTP POST上传";
      }
      $upload_path ="Uploads/";
      $new_p=$upload_path.$fileimg.'/'.microtime(TRUE).".".$type;
      $res=move_uploaded_file($file['tmp_name'],$new_p);
      if($res){
        $mes['code']=1;
        $mes['link']="/".$new_p;
        $msg['message']='成功';
        return $mes;

      }
      else{
        $mes['code']=996;
        $mes['link']="/".$new_p;
        $msg['message']='添加失败';
        return $mes;

      }
  }
}